﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace MethodInvoker
{
    public delegate void TimerCallback(object state);
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //TaskInfo ti = new TaskInfo();

            //// 调创建 Timer 对象，将一个回调方法传给它，每个一秒调用一次
            //Timer tm = new Timer(ShowTime, ti, 0, 1000);
            //Console.ReadKey();
            //tm.Dispose();

        }

        static void ShowTime(object num)
        {
            TaskInfo obj = num as TaskInfo;
            obj.count++;
            Console.WriteLine("{0},{1}", obj.count, DateTime.Now);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Thread th = new Thread(new ParameterizedThreadStart(GetPath));
            th.IsBackground = true;
            th.Start("100");
        }
        void GetPath(object str)
        {
            int j = int.Parse(str.ToString());
            for (int i = 0; i <= j; i++)
            {
                //Thread.Sleep(100);
                //Invoke(new MethodInvoker(delegate () { this.progressBar3.Value = i; }));
                //this.Invoke(new MethodInvoker(delegate () { this.label2.Text = "当前已完成" + i.ToString() + "%,请稍等……"; }));
                //if (i == 100)
                //{
                //    this.Invoke(new MethodInvoker(delegate () { this.label2.Text = "完成"; }));
                //    if (DialogResult.OK == MessageBox.Show("成功3", "测试3", MessageBoxButtons.OKCancel, MessageBoxIcon.Question))
                //    {
                //        this.Close();
                //    }
                //}
            }
        }
    }

    class TaskInfo
    {
        public int count = 0; // 回调方法被执行的次数
    }
}
